﻿Public Class frmConfig

    Public Property ServerURL As String
    Public Property UserName As String
    Public Property Password As String
    Public Property ReportPath As String
    Public Property Timeout As Integer
    Public Property dictReports As Generic.Dictionary(Of String, String)
    Public Property ReportDir As String
    Public Property Project As Project.clsProject
    Public Property ShowAllOwnerRecords As Boolean

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

    End Sub

    Public Sub New(_ServerURL As String, _UserName As String, _Password As String, _ReportPath As String, _ReportDir As String, _Timeout As Integer, _Project As Project.clsProject, _ShowAllOwnerRecords As Boolean, _dictReports As Generic.Dictionary(Of String, String))

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        ServerURL = _ServerURL
        UserName = _UserName
        Password = _Password
        ReportPath = _ReportPath
        'report path must begin and end with slashes
        If Not ReportPath.StartsWith("/") Then ReportPath = "/" & ReportPath
        If Not ReportPath.EndsWith("/") Then ReportPath &= "/"
        If Not String.IsNullOrEmpty(ReportDir) AndAlso Not ReportDir.EndsWith("\") Then ReportDir &= "\"
        ReportDir = _ReportDir
        Timeout = _Timeout
        Project = _Project
        ShowAllOwnerRecords = _ShowAllOwnerRecords
        dictReports = _dictReports
    End Sub

    Private Sub chkWindowsSecurity_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkWindowsSecurity.CheckedChanged
        ResetWindowsSecurity()
    End Sub

    Private Sub ResetWindowsSecurity()
        If chkWindowsSecurity.Checked Then
            txtUserName.Text = ""
            txtPassword.Text = ""
            lblUserName.Visible = False
            txtUserName.Visible = False
            lblPassword.Visible = False
            txtPassword.Visible = False
        Else
            lblUserName.Visible = True
            txtUserName.Visible = True
            lblPassword.Visible = True
            txtPassword.Visible = True
        End If
    End Sub

    Private Sub frmConfig_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        txtServer.Text = _ServerURL
        If _UserName = "" Then
            chkWindowsSecurity.Checked = True
        Else
            chkWindowsSecurity.Checked = False
            txtUserName.Text = _UserName
            txtPassword.Text = _Password
        End If
        txtPath.Text = _ReportPath
        txtTimeout.Text = Timeout
        txtDirectory.Text = _ReportDir
        With dgReports
            .Columns.Add("Report Description", "Report Description")
            .Columns.Add("Report Name on Server or Filename", "Report Name on Server or Filename")
            .ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
            .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft
            .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
            .AllowUserToAddRows = True
            .AllowUserToDeleteRows = True
            .AllowUserToOrderColumns = False
            .AllowUserToResizeColumns = False
            .AllowUserToResizeRows = False
            .Columns(0).SortMode = DataGridViewColumnSortMode.NotSortable
            .Columns(1).SortMode = DataGridViewColumnSortMode.NotSortable
            For Each s As String In _dictReports.Keys
                .Rows.Add(s, _dictReports(s))
            Next
        End With
    End Sub

    Private Sub btnOK_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnOK.Click
        ServerURL = txtServer.Text
        UserName = txtUserName.Text
        Password = txtPassword.Text
        ReportPath = txtPath.Text
        ReportDir = txtDirectory.Text
        Timeout = Val(txtTimeout.Text)
        dictReports.Clear()
        With dgReports
            For r As Integer = 0 To .Rows.Count - 2
                Dim rptDesc As String = .Item(0, r).Value
                rptDesc = rptDesc.Replace(":", ";").Replace("=", "-") 'necessary because : is delimiter in ini file
                Dim rptName As String = .Item(1, r).Value
                rptName = rptName.Replace(":", ";").Replace("=", "-")
                If Not String.IsNullOrEmpty(rptDesc) AndAlso Not String.IsNullOrEmpty(rptName) AndAlso Not dictReports.ContainsKey(rptDesc) Then
                    dictReports.Add(rptDesc, rptName)
                End If
            Next
        End With
    End Sub

    Private Sub btnView_Click(sender As System.Object, e As System.EventArgs) Handles btnView.Click
        Dim ReportName As String = dgReports.Item(1, dgReports.CurrentCellAddress.Y).Value
        If String.IsNullOrEmpty(ReportName) Then Exit Sub
        With New frmReporting(txtServer.Text, txtUserName.Text, txtPassword.Text, txtPath.Text, txtDirectory.Text, txtTimeout.Text, ReportName, Project, ShowAllOwnerRecords)
            .ShowDialog(Me)
        End With
    End Sub

    Private Sub btnBrowse_Click(sender As System.Object, e As System.EventArgs) Handles btnBrowse.Click
        With New FolderBrowserDialog
            .SelectedPath = txtDirectory.Text
            If .ShowDialog(Me) = Windows.Forms.DialogResult.OK Then
                txtDirectory.Text = .SelectedPath & "\"
            End If
            .Dispose()
        End With
    End Sub

    Private Sub btnEdit_Click(sender As System.Object, e As System.EventArgs) Handles btnEdit.Click
        Dim ReportName As String = dgReports.Item(1, dgReports.CurrentCellAddress.Y).Value
        If String.IsNullOrEmpty(ReportName) Then Exit Sub
        RunProgram("WRDB6",
                   "MSReportBuilder.exe",
                   "C:\Program Files (x86)\Microsoft SQL Server\Report Builder 2.0\",
                   """" & txtDirectory.Text & ReportName & """",
                   "Microsoft Report Builder is a free program available from http://download.microsoft.com; search for 'Report Builder 2.0'. Note that only this version will create RDL files compatible with SQL Server 2008 and this version of the WRDB Report Viewer. You must have Administrator privileges on your computer to install the program.")
    End Sub

    Private Sub btnSelect_Click(sender As Object, e As EventArgs) Handles btnSelect.Click

        If String.IsNullOrEmpty(txtDirectory.Text) OrElse Not My.Computer.FileSystem.DirectoryExists(txtDirectory.Text) Then
            WarningMsg("A valid Report Directory must be entered above. Note that it is not possible to select reports that are stored on a Report Server (the names must be typed).")
        Else
            With New OpenFileDialog
                .AddExtension = True
                .CheckFileExists = True
                .DefaultExt = ".rdl"
                .Filter = "Report Builder files|*.rdl"
                .InitialDirectory = txtDirectory.Text
                .Multiselect = False
                If .ShowDialog = Windows.Forms.DialogResult.OK Then
                    Dim fn As String = .SafeFileName
                    With dgReports
                        If .CurrentRow Is Nothing OrElse .CurrentRow.IsNewRow Then
                            .Rows.Add()
                            .CurrentCell = .Item(0, .RowCount - 2)
                        End If
                        If .CurrentRow.Cells(0).Value = "" Then .CurrentRow.Cells(0).Value = IO.Path.GetFileNameWithoutExtension(fn)
                        .CurrentRow.Cells(1).Value = fn
                    End With
                End If
            End With
        End If
    End Sub
End Class